package action.back;

import javax.servlet.http.Cookie;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import service.OrgManager;
import utils.StringHelper;
import action.vo.LoginInfo;
import config.Constant;

public class LogonAction extends BaseAction {
	
	public String prepareLogin() throws Exception {
		return "prepareLogin";
	}
	
	public String login() throws Exception {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			String loginname = null;
			String password = null;
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("loginname")) {
					loginname = cookie.getValue();
				} else if (cookie.getName().equals("password")) {
					password = cookie.getValue();
				}
			}
			String savepassword = request.getParameter("savepassword");
			if(StringUtils.isEmpty(loginname)){
				loginname = request.getParameter("loginname");
			}
			if(StringUtils.isEmpty(password)){
				password = request.getParameter("password");
			}
			if (procLogin(loginname, password, String.valueOf(StringHelper.parseBoolean(savepassword))) == true) {
				String redirect = request.getParameter("redirect");
				if(redirect !=null){
					redirect(redirect);
				}else{
					return APP_INDEX;
				}
				
			}
		}
		return APP_LOGIN;
	}

	public String logout() throws Exception {

		session.removeAttribute(Constant.LOGININFO);

		Cookie c1 = new Cookie("loginname", null);
		c1.setPath(getContextPath(request));
		c1.setMaxAge(0);
		Cookie c2 = new Cookie("password", null);
		c2.setPath(getContextPath(request));
		c2.setMaxAge(0);
		response.addCookie(c1);
		response.addCookie(c2);
		
		return APP_LOGIN;
	}

	/**
	 * 处理系统登录。
	 * 
	 * @return
	 * @throws Exception 
	 * @throws java.lang.Exception
	 */
	public void handleLogin() throws Exception {
		String loginname = request.getParameter("loginname");
		String password = request.getParameter("password");
		String savepassword = request.getParameter("savepassword");

		if (procLogin(loginname, password, savepassword)) {
			renderJson("{success:true}");
		} else {
			renderJson("{failure:true,msg:'登录帐号或密码不对。'}");
		}
	}

	@Autowired
	private OrgManager orgManager;
	/**
	 * 处理登录系统
	 * 
	 * @param username
	 * @param password
	 * @param savepassword
	 *            是否保存密码，值为true时保存密码，否则不保存
	 * @return
	 * @throws java.lang.Exception
	 */
	private boolean procLogin(String loginname, String password,
			String savepassword) throws Exception {
		LoginInfo loginInfo =  orgManager.authUser(loginname, password);
		
		if (loginInfo != null) {
			session.setAttribute(Constant.LOGININFO, loginInfo);
			if (savepassword != null && savepassword.equals(Constant.TRUE)) {
				Cookie c1 = new Cookie("loginname", loginname);
				c1.setMaxAge(24 * 3600);
				c1.setPath(getContextPath(request));
				Cookie c2 = new Cookie("password", password);
				c2.setMaxAge(24 * 3600);
				c2.setPath(getContextPath(request));
				response.addCookie(c1);
				response.addCookie(c2);
			}
			return true;
		} else {
			return false;
		}
	}

}
